#include "ouden.h"

namespace ouden
{
	
	//Return the singleton instance of the engine
	ODN_Engine* getEngine()
	{
		return ODN_Engine::Instance();
	}
	
	
	//Initialise the engine with renderer settings 
	bool ODN_Engine::Init(char* Title, int Width, int Height, int BitsPerPixel, bool Fullscreen)
	{
		
		//Create the renderer window 
		gfx::getRenderer()->CreateRendererWindow(Title, Width, Height, BitsPerPixel, Fullscreen);
		
		
		
	}
	
	//Run the main loop 
	void ODN_Engine::Run()
	{
		
		//Get renderer object
		gfx::ODN_Renderer* Renderer = getRenderer();
		
		//Get input handler object
		input::ODN_Input* Input = input::getInputHandler();
		
		//Get timer object 
		ODN_Timer* Timer = getTimer();
		
		bool running = true;
		
		ODN_Mesh m;
		m.loadMesh();
		
		//Main loop 
	    while (running) {
			
			
			//Check for windows messages
			if (Renderer->CheckMessages()) continue;
			
			//Make sure the renderer is currently active
			if (!Renderer->isActive()) continue;
	
			//Check if the Engine has recieved a quit message
			if (Renderer->isQuit()) running = false;
			
			//OpenGL error checking
			int r = glGetError();
			if (r != GL_NO_ERROR) { std::cout << "OpenGL Error " << r << std::endl; }
			
			//Retrieve input from input handler
			Input->CheckInput();
			
			//Check for escape key
			if (Input->isKeyDown(VK_ESCAPE)) running = false;
			
			//Update the timer 
			Timer->Update();
			
			Timer->setElapsed(100);
			
			//Update the engine
			Update();
			
			//m.Draw();
			
			glColor4f(1, 1, 1, 1);
			
			glBegin(GL_POINTS);
				
				glVertex3f(0, 0, 0);
				
			glEnd();
			
			//Render the scene
			Renderer->DrawScene(); 
			
		}
		
	}
	
	//Updates and draws all of the scenes in the engine 
	bool ODN_Engine::Update()
	{
		for (int i=0;i<scenelist.size();i++) {
			if (scenelist[i]->isActive()) scenelist[i]->Update();
			if (scenelist[i]->isVisible()) scenelist[i]->Draw();
		}	
	}
	
}
